package cn.tsvtc.shop.Service.impl;/**
 * @Author Athena
 * @Date 2020/11/2 18:52
 * @Version 1.0
 * @Description no description
 */

import cn.tsvtc.shop.Service.ProductTypeService;
import cn.tsvtc.shop.mapper.ProductTypeMapper;
import cn.tsvtc.shop.pojo.ProductType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: Athena
 * @date: 2020/11/2 18:52
 * @description:
 */
@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    private RedisTemplate redisTemplate;

    private ProductTypeMapper productTypeMapper;

    @Autowired
    public ProductTypeServiceImpl(ProductTypeMapper productTypeMapper,RedisTemplate redisTemplate){
        this.productTypeMapper=productTypeMapper;
        this.redisTemplate=redisTemplate;
    }



    /**
     * 获取全部商品分类信息
     *
     * @return
     */
    @Override
    public List<ProductType> findAll() {
        List<ProductType> types=new ArrayList<>();
        //从redis中获取
        Map<Long,ProductType> map = redisTemplate.opsForHash().entries("t_product_type");
        for(Map.Entry<Long,ProductType> entry : map.entrySet()){
            Long mapKey = entry.getKey();
            ProductType mapValue = entry.getValue();
            types.add(mapValue);
        }
        if(types.size()==0){
            types = productTypeMapper.findAll();
        }
        return types;
    }
}
